package com.hb.trade.server.project.calibration.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hb.core.pojo.Attachments;
import com.hb.core.pojo.parent.PageResult;
import com.hb.core.pojo.project.calibration.dto.BidWinResultSupplyDTO;
import com.hb.core.pojo.project.calibration.entity.BidWinResultSupply;
import com.hb.core.pojo.project.calibration.mapper.BidWinResultSupplyMapper;
import com.hb.trade.server.project.calibration.service.BidWinResultSupplyService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 中标通知书关联投标人表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-09-28
 */
@Service
public class BidWinResultSupplyServiceImpl extends ServiceImpl<BidWinResultSupplyMapper, BidWinResultSupply> implements BidWinResultSupplyService {

    @Override
    public List<BidWinResultSupply> listSupplies(Long successfulNoticeId) {
        if (null == successfulNoticeId) {
            return CollUtil.list(false);
        }
        return baseMapper.listSupplies(successfulNoticeId, null);
    }

    @Override
    public boolean updateBidPrice(Long id, BigDecimal amount, Attachments attachments) {
        if (null == id || null == amount) {
            return false;
        }
        return baseMapper.updateBidPrice(id, amount, attachments) > 0;
    }

    /**
     *
     * @Description: 根据中标结果id查询多个中标人
     * @Author: zhongdl
     * @Date:2021/12/20 10:31
     * @param winResultId:
     * @return: {@link List<  BidWinResultSupply > }
     **/
    @Override
    public List<BidWinResultSupplyDTO> listSuccessfulNoticeSupply(Long winResultId) {
        List<BidWinResultSupplyDTO> bidWinResultSupplyDTOS = baseMapper.listSuccessfulNoticeSupply(winResultId);
        if(CollUtil.isNotEmpty(bidWinResultSupplyDTOS)){
            for (BidWinResultSupplyDTO bidWinResultSupplyDTO:bidWinResultSupplyDTOS) {
                bidWinResultSupplyDTO.setOpenFlag(null == bidWinResultSupplyDTO.getOrgId() ? 2 :1);
            }
        }
        return bidWinResultSupplyDTOS;
    }

    /**
     *
     * @Description: 删除中标人公示下的中标人
     * @Author: zhongdl
     * @Date:2021/12/20 10:38
     * @param winResultId:
     * @return: {@link Boolean }
     **/
    @Override
    public Boolean deleteSuccessfulNoticeSupplyByWinResultId(Long winResultId) {
        QueryWrapper<BidWinResultSupply> supplyWrapper = new QueryWrapper<>();
        supplyWrapper.eq("win_result_id", winResultId);
        return this.remove(supplyWrapper);
    }

    /**
     *
     * @Description: 查询中标结果公示包含的标段信息
     * @Author: zhongdl
     * @Date:2022/1/5 17:09
     * @param successfulNoticeSupply:
     * @return: {@link PageResult<  BidWinResultSupplyDTO > }
     **/
    @Override
    public PageResult<BidWinResultSupplyDTO> pageIncludeBidSection(BidWinResultSupplyDTO successfulNoticeSupply) {
        Page<BidWinResultSupplyDTO> pages = successfulNoticeSupply.pages();
        baseMapper.pageIncludeBidSection(pages, successfulNoticeSupply.getId());
        return PageResult.get(pages);
    }

    /**
     * 查询当前企业是否中标
     * @author tian
     * @date 2022-3-1 9:21
     * @param sectionId
     * @param orgId
     * @return java.lang.Boolean
     */
    @Override
    public Boolean getWinResultSupplyBySectionId(Long sectionId, Long orgId) {
        BidWinResultSupplyDTO winResultSupply = baseMapper.getWinResultSupplyBySectionId(sectionId);
        if (null != winResultSupply && winResultSupply.getOrgId().equals(orgId)) {
            return true;
        }
        return false;
    }


    /**
     * @Description: 根据标段ID查询详情
     *
     * @author yuezheng
     * @param sectionIdList
     * @return java.util.List<com.hb.core.pojo.project.calibration.entity.BidWinResultSupply>
     * @date 2022/6/27 19:17
     */
    @Override
    public List<BidWinResultSupply> selectResultSupplyBySectionIdList(List<Long> sectionIdList) {
        LambdaQueryWrapper<BidWinResultSupply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(BidWinResultSupply::getSectionId,sectionIdList);
        return this.baseMapper.selectList(queryWrapper);
    }
}
